package com.cpi.basilos.location.service;

import org.apache.log4j.Logger;

import com.cpi.basilos.corntype.converter.CornTypeConverter;
import com.cpi.basilos.corntype.dao.CornTypeDAO;
import com.cpi.basilos.corntype.dto.CornTypeDTO;
import com.cpi.basilos.corntype.exception.CornTypeException;
import com.cpi.basilos.corntype.view.CornTypeView;
import com.cpi.basilos.location.converter.LocationConverter;
import com.cpi.basilos.location.dao.LocationDAO;
import com.cpi.basilos.location.dto.LocationDTO;
import com.cpi.basilos.location.exception.LocationException;
import com.cpi.basilos.location.view.LocationView;
import com.cs.common.exception.CSException;

public class LocationService {
	private static org.apache.log4j.Logger logger = Logger.getLogger(LocationService.class);
	
	public LocationView getLocation(int warehouseId, String locationId)throws LocationException{
		LocationView locationView = null;
		CornTypeView cornTypeView = null;
		LocationDAO locationDAO = null;
		CornTypeDAO cornTypeDAO = null;
		LocationDTO locationDTO = null;
		CornTypeDTO cornTypeDTO = null;
		LocationConverter locationConverter = null;
		CornTypeConverter cornTypeConverter = null;
		try{
			locationDAO = new LocationDAO();
			cornTypeDAO = new CornTypeDAO();
			locationConverter = new LocationConverter();
			cornTypeConverter = new CornTypeConverter();
			locationDTO = locationDAO.getLocation(warehouseId, locationId);
			cornTypeDTO = cornTypeDAO.cornTypeList(locationDTO.getCornTypeId());
			locationView = locationConverter.convertDTOToView(locationDTO);
			cornTypeView = cornTypeConverter.convertDTOToView(cornTypeDTO);
			locationView.setCornTypeView(cornTypeView);
		}catch(CornTypeException cornTypeException){
			throw new LocationException(cornTypeException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
		}catch(LocationException locationException){
			throw locationException;
		}catch(Exception exception){
			LocationException locationException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			locationException = new LocationException(exception, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw locationException;
		}
		return locationView;
	}
}